﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RookieSharpLibs.Ui
{
    public class ListViewOperate
    {
        private ListView ListView;
        public ListViewOperate(ListViewParameter para)
        {
            ListView = para.ListView;
            ListView.CheckBoxes = true;
            ListView.FullRowSelect = true;
            ListView.View = View.Details;
            for (int idx = 0; idx < para.Columns.Count;idx++ )
            {
                ColumnHeader header = new ColumnHeader();
                
                //header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                header.Text = para.Columns[idx].Text;
                header.Width = para.Columns[idx].Width;
                ListView.Columns.Add(header);
            }
            if (null != para.ContextMenu)
            {
                ListView.ContextMenuStrip = para.ContextMenu;
            }
            
        }
        public ListViewItem GetSelectedItem(int index)
        {
            if (ListView.SelectedItems.Count == 0)
            {
                return null;
            }
            return ListView.SelectedItems[0];
        }
        public int GetSelectedItemCount()
        {
            return ListView.SelectedItems.Count;
        }
        public void RemoveItem(int index)
        {
            ListView.Items.RemoveAt(index);
        }
        public void AddItem(ListViewItem newItem)
        {
            ListView.Items.Add(newItem);
        }
        public void AddItem(ListViewItem newItem, int index)
        {
            ListView.Items.Insert(index, newItem);
        }
        public int GetItemsCount()
        {
            return ListView.Items.Count;
        }
        public ListViewItem GetItem(int idx)
        {
            return ListView.Items[idx];

        }
        public void DeleteSelectedItems()
        {
            int nCount = ListView.SelectedItems.Count;
            for (int idx = nCount - 1; idx >= 0;idx-- )
            {
                ListView.Items.RemoveAt(ListView.SelectedItems[idx].Index);
            }
        }

    }
}
